<?php
namespace app\index\controller;

use app\index\model\FlowBillModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowSetModel;
use app\index\model\FlowShareModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\service\FlowService;
use app\index\service\UserService;
use think\Db;
use think\Exception;
use think\Request;

class Daily extends Common
{

    public function list_bill_create(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name,e.name as enginname')
            ->join('user user','user.id=bill.created_user')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        $data['is_modeid'] = isset($param['modeid']) ? $param['modeid'] : '';
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bill_hand(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','=', 0];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name,e.name as enginname')
            ->join('user user','user.id=bill.created_user')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bill_finish(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','in', [1,2,3,5]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name,e.name as enginname')
            ->join('user user','user.id=bill.created_user')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bill_turn(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 0];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name,e.name as enginname')
            ->join('user user','user.id=bill.created_user')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function del_bill_turn(Request $request){
        $param = $request->post();
        $data = FlowBillModel::where(['id'=>$param['id']])->find();
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        if($data['created_user'] != $this->userinfo['id']){
            return outputJson(4000,lang('code_4000'));
        }
        if($data['isturn'] != 0){
            return outputJson(4000,lang('code_4000'));
        }


        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            Db::name($data['modenum'])->where(['id'=>$data['mid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_bill_share(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];

        $whereShare = "";
        if(isset($param['types']) && !empty($param['types'])){
            if($param['types'] == 'tome'){
                $whereShare = Db::raw("find_in_set(".$this->userinfo['id'].",domer)");
            }elseif($param['types'] == 'meto'){
                $whereShare = Db::raw("share.uid=".$this->userinfo['id']);
            }else{
                $whereShare = Db::raw("share.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            }
        }else{
            $whereShare = Db::raw("share.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['created_at_share']) && !empty($param['created_at_share'])){
            $seadt = explode(' - ', $param['created_at_share']);
            $where[] = ['share.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['fenname']) && !empty($param['fenname'])){
            $where[] = ['fen.realname','like', '%'.$param['fenname'].'%'];
        }


        $geiWhere = '';
        if(isset($param['geiname']) && !empty($param['geiname'])){
            $getId = UserModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where('realname','like','%'.$param['geiname'].'%')->column('id');
            foreach ($getId as $gid){
                if(empty($geiWhere)){
                    $geiWhere = "find_in_set(".$gid.",domer)";
                }else{
                    $geiWhere .= " or find_in_set(".$gid.",domer)";
                }
            }
            if(empty($geiWhere)){
                $where[] = ['share.id','lt', 0];
            }
        }


        $data['data'] = FlowShareModel::alias('share')
            ->field('bill.*,share.uid,share.domer,share.id as shareid,share.created_at as created_at_share,share.explain as explain_share
            ,user.realname as created_user_name,e.name as enginname')
            ->leftJoin('flow_bill bill','share.billid = bill.id')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->leftJoin('user fen','fen.id=share.uid')
            ->where($where)
            ->where($geiWhere)
            ->where($whereShare)
            ->order('share.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['uid_name'] = UserModel::get_realname_by_pk($item['uid']);
                $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();

        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_add_bill(Request $request){
        $param = $request->post();
        $where = [];
        $where[] = ['isxian','=', 1];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }

        $whereTeshu = "";
        if($this->userinfo['token_roletype'] == 1 && in_array(25, explode(',', $this->userinfo['roleid']))){
//            $whereTeshu = "id in (2,58,61,3,12,55,59)";
        }

        $info = [];
        $set = FlowSetModel::where($where)->where($whereTeshu)->order('sort ASC')->select()->toArray();
        foreach ($set as $key=>&$item){
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$item['id']])->find();
            if(!$exist){
                $insid = FlowClassModel::insertGetId(['comid'=>$this->userinfo['comid'], 'setid'=>$item['id']]);
                $exist = FlowClassModel::where(['id'=>$insid])->find();
            }
            $item['id'] = $exist['id'];
            $item['status'] = $exist['status'];
            if(isset($param['status']) && $param['status'] != ''){
                if($param['status'] != $item['status']){
                    unset($set[$key]);
                    continue;
                }
            }

            if($item['status'] == 0){
                unset($set[$key]);
                continue;
            }

            $item['ispublish'] = false;
            $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$item['id'],'status'=>1,'deleted_user'=>0])->find();
            if($course){
                $item['ispublish'] = true;
                $step = json_decode($course['step'], true);
                if($step && is_array($step)){
                    if((new FlowService())->check_course_start_users($step)){
                        $info[] = $item;
                    }
                }
            }
        }

        $list = [];
        $nav = (new UserService())->get_flowset_nav();
        foreach ($nav as $k=>&$v){
            $children = [];
            foreach ($info as $vv){
                if($vv['typename'] == $k){
                    $children[] = $vv;
                }
            }
            if($children){
                $list[] = ['name'=>$v, 'children'=>$children];
            }
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_add_bill_mob(Request $request){
        $param = $request->post();
        $where = [];
        $where[] = ['isxian','=', 1];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }


        $info = [];
        $set = FlowSetModel::where($where)->order('sort ASC')->select()->toArray();
        foreach ($set as $key=>&$item){
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$item['id']])->find();
            if(!$exist){
                $insid = FlowClassModel::insertGetId(['comid'=>$this->userinfo['comid'], 'setid'=>$item['id']]);
                $exist = FlowClassModel::where(['id'=>$insid])->find();
            }
            $item['modeid'] = $item['id'];
            $item['id'] = $exist['id'];
            $item['status'] = $exist['status'];
            if(isset($param['status']) && $param['status'] != ''){
                if($param['status'] != $item['status']){
                    unset($set[$key]);
                    continue;
                }
            }

            $item['ispublish'] = false;
            $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$item['id'],'status'=>1,'deleted_user'=>0])->find();
            if($course){
                $item['ispublish'] = true;
                $step = json_decode($course['step'], true);
                if($step && is_array($step)){
                    if((new FlowService())->check_course_start_users($step)){
                        $info[] = $item;
                    }
                }
            }
        }

        $list = [];

        $nav = (new UserService())->get_flowset_nav();
        foreach ($nav as $k=>&$v){
            $children = [];
            foreach ($info as $vv){
                if($vv['typename'] == $k){
                    $children[] = $vv;
                }
            }
            if($children){
                $list[] = ['name'=>$v, 'children'=>$children];
            }
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_flow_hand(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','=', 0];
        $where[] = ['task.deleted_user','=', 0];
        $where[] = ['task.uid','=', $this->userinfo['id']];
        $where[] = ['task.state','=', 1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['created_at_song']) && !empty($param['created_at_song'])){
            $seadt = explode(' - ', $param['created_at_song']);
            $where[] = ['task.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }

        $data['data'] = FlowTaskModel::alias('task')
            ->field('bill.*,task.created_at as created_at_song,user.realname as created_user_name,
            task.tuitype,task.tiaotype,e.name as enginname')
            ->join('flow_bill bill','bill.id=task.billid')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->where($where)
            ->order('task.created_at DESC')
            ->group('bill.id')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();



        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_finish(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['shen.uid','=', $this->userinfo['id']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at_song']) && !empty($param['created_at_song'])){
            $seadt = explode(' - ', $param['created_at_song']);
            $where[] = ['shen.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['updated_at_shen']) && !empty($param['updated_at_shen'])){
            $seadt = explode(' - ', $param['updated_at_shen']);
            $where[] = ['shen.updated_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }

        $data['data'] = FlowTaskShenModel::alias('shen')
            ->field('bill.*,user.realname as created_user_name,e.name as enginname')
            ->join('flow_bill bill','bill.id=shen.billid')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->where($where)
            ->order('shen.id DESC')
            ->group('bill.id')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                $shen = FlowTaskShenModel::where(['billid'=>$item['id'],'uid'=>$this->userinfo['id']])->order('id DESC')->find();
                $item['shenrecall'] = $shen['recall'];
                $item['created_at_song'] = $shen['created_at'];
                $item['updated_at_shen'] = $shen['updated_at'];
                $item['shenname'] = $shen['name'];

                $item['shenrecall'] = 0;
                if($item['isturn'] == 1 && $item['status'] == 0){
                    $taskShen = FlowTaskShenModel::where(['billid'=>$item['id'],'uid'=>$this->userinfo['id'],'recall'=>1])
                        ->where('opt','in',['add','zhuan','agree','tui','next'])->order('id DESC')->find();
                    if($taskShen){
                        $item['shenrecall'] = 1;
                    }
                }

                return $item;
            })->toArray();



        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_promer(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
//        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];

        $wherePromer = "";
        if(isset($param['types']) && !empty($param['types'])){
            if($param['types'] == 'tome'){
                $wherePromer = Db::raw("find_in_set(".$this->userinfo['id'].",domer)");
            }elseif($param['types'] == 'meto'){
                $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']);
            }else{
                $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            }
        }else{
            $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
        }

        if(isset($param['fenname']) && !empty($param['fenname'])){
            $where[] = ['fen.realname','like', '%'.$param['fenname'].'%'];
        }


        $geiWhere = '';
        if(isset($param['geiname']) && !empty($param['geiname'])){
            $getId = UserModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where('realname','like','%'.$param['geiname'].'%')->column('id');
            foreach ($getId as $gid){
                if(empty($geiWhere)){
                    $geiWhere = "find_in_set(".$gid.",domer)";
                }else{
                    $geiWhere .= " or find_in_set(".$gid.",domer)";
                }
            }
            if(empty($geiWhere)){
                $where[] = ['promer.id','lt', 0];
            }
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.summary','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['created_at_promer']) && !empty($param['created_at_promer'])){
            $seadt = explode(' - ', $param['created_at_promer']);
            $where[] = ['promer.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }

        $data['data'] = FlowPromerModel::alias('promer')
            ->field('bill.*,promer.uid,promer.domer,promer.id as promerid,promer.created_at as created_at_promer
            ,user.realname as created_user_name,e.name as enginname')
            ->join('flow_bill bill','promer.billid = bill.id')
            ->leftJoin('engin e','e.id=bill.enginid')
            ->join('user user','user.id=bill.created_user')
            ->join('user fen','fen.id=promer.uid')
            ->where($where)
            ->where($wherePromer)
            ->where($geiWhere)
            ->order('promer.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['uid_name'] = UserModel::get_realname_by_pk($item['uid']);
                $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();

        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function list_todo(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['uid','=',$this->userinfo['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['mess','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['mess']) && !empty($param['mess'])){
            $where[] = ['mess','like', '%'.$param['mess'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['status','=', $param['status']];
        }
        if(isset($param['xiaolei']) && $param['xiaolei'] != ''){
            $where[] = ['xiaolei','=', $param['xiaolei']];
        }


        if(isset($param['tododt']) && !empty($param['tododt'])){
            $seadt = explode(' - ', $param['tododt']);
            $where[] = ['tododt','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = TodoModel::where($where)
            ->order('id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['xiaolei_name'] = $item['xiaolei'] == 2 ? '预警消息' : '通知消息';
                $item['status_name'] = $item['status'] > 0 ? '已读' : '未读';
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function allread_todo(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->update([
                    'status' => 1,
                    'readdt' => date('Y-m-d H:i:s')
                ]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(2000,lang('code_2000'));
    }




}
